#include "qcheckboxhost.h"



#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QCheckBox>
#include <QVariant>

using namespace SHAREDLIB;

QCheckBoxHost::QCheckBoxHost(QObject *object):
    QButtonHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","tristate");
        pro->setAttribute("property_name",tr("Tristate"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","stateChanged");
        pro->setAttribute("property_name",tr("StateChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("stateChanged","");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Stylesheet");
    if(pro!=NULL)
    {
        pro->setAttribute("name","stylesheet_text");
        pro->setAttribute("property_name",tr("Text"));
        pro->setAttribute("group","Style Sheet");

        StyleItems items;
        tagStyleItem item;

        item.m_string="normal";
        item.m_text=tr("Normal");
        items.append(item);


        item.m_string="hover";
        item.m_text=tr("Hover");
        items.append(item);

        item.m_string="pressed";
        item.m_text=tr("Pressed");
        items.append(item);

        item.m_string="checked";
        item.m_text=tr("Checked");
        items.append(item);

        item.m_string="checked:hover";
        item.m_text=tr("Checked-Hover");
        items.append(item);

        item.m_string="checked:pressed";
        item.m_text=tr("Checked-Pressed");
        items.append(item);

        QVariant v;
        v.setValue<StyleItems>(items);
        pro->setAttribute("items",v);

        m_object->setProperty("stylesheet_text",QVariant());
        addProperty(pro);
    }

    appendMemery("tristate");

    QCheckBox* box=(QCheckBox*)m_object;
    connect(box,SIGNAL(stateChanged(int)),this,SLOT(stateChanged(int)));
}

void QCheckBoxHost::setTristate(bool tristate)
{
    setPropertyValue("tristate",tristate);
}

bool QCheckBoxHost::tristate()
{
    return getPropertyValue("tristate").toBool();
}

void QCheckBoxHost::stateChanged(int state)
{
    QString code=getPropertyValue("stateChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("state",QString::number(state));
        emit_exec(code,param);
    }
}
